﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 01.04.2008
 * Zeit: 13:09
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using System.Collections.Generic;
using MSAst = Microsoft.Scripting.Ast;
using Microsoft.Scripting;

namespace Gamadus.Engine.Compiler.Runtime
{
	/// <summary>
	/// Description of RuntimeMgr.
	/// </summary>
	public class RuntimeMgr : SingletonManager<RuntimeMgr>
	{
		public Dictionary<string, MSAst.VariableExpression> m_globals;
		MSAst.LambdaBuilder m_context;
		
		public void Init()
		{
			m_globals = new Dictionary<string, MSAst.VariableExpression>();
			m_context = MSAst.Ast.Lambda("<global>", typeof(object));
		}
		
		public MSAst.LambdaExpression Finish(MSAst.Expression body)
		{
			m_context.Body = body;
            return m_context.MakeLambda(); 
		}
		
		public MSAst.VariableExpression GetOrMakeGlobal(string name, Type type)
		{
			MSAst.VariableExpression variable;
            if (m_globals.TryGetValue(name, out variable)) {
                return variable;
            }
            variable = m_context.CreateLocalVariable(SymbolTable.StringToId(name), type);
            m_globals[name] = variable;
            return variable;
		}
	}
}
